/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server;

import Model.Reader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Admin
 */
public class ServerReader {

   

   
        ObjectInputStream inObject;
        ObjectOutputStream outObject;
    
    public ServerReader( ObjectInputStream inObject, ObjectOutputStream outObject)
    {
        this.inObject = inObject;
        this.outObject =outObject;
        
    }

    void showAllReader() {
         ArrayList<Reader> arrReader = new ArrayList<>();
        arrReader=ShowAllReader();
        
        try {
            outObject.writeObject(arrReader);
         
        } catch (IOException ex) {
                System.out.println(ex);
        }
    }
     void deleteReader() {
        try {
              Reader reader = (Reader) inObject.readObject();//nhan doi tuong login do client goi         
              outObject.writeObject(DeleteReader(reader));      //goi thong bao cho client                
            } catch (IOException | ClassNotFoundException ex) {
                    System.out.println(ex); //in loi
            }
    }
    void editReader() {
           try {
              Reader reader = (Reader) inObject.readObject();//nhan doi tuong login do client goi         
              outObject.writeObject(EditReader(reader));      //goi thong bao cho client                
            } catch (IOException | ClassNotFoundException ex) {
                    System.out.println(ex);
            }
    }
    
    void addReader() {
       try {
            Reader reader = (Reader) inObject.readObject();// nhan book tu client
            outObject.writeObject(AddReader(reader));      //goi thong bao cho client
        } catch (IOException | ClassNotFoundException ex) {
            System.out.println(ex);
        }  
    }
    private ArrayList<Reader> ShowAllReader() {
         Connection con = MyConnect.getConnect();
        ArrayList<Reader> arrReader = new ArrayList<>();
        try {
            Statement stm = con.createStatement();
            String sql = "select * from Readers";
            ResultSet rs = stm.executeQuery(sql);
            while (rs.next()) {
                Reader reader = new Reader(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5));            
                arrReader.add(reader);
                    
                
            }
            return arrReader;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "ShowAllReader: " + e.getMessage());
            return null;
        }
    }

   
    
    
     public boolean DeleteReader(Reader reader) {
        try {
            Connection cn = MyConnect.getConnect();
            String sql = "delete from Readers where ReaderID=?";
            PreparedStatement pst = cn.prepareStatement(sql);
            pst.setString(1,reader.getReaderID());
            pst.executeUpdate();
            pst.close();
            cn.close();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Delete Reader" + ex.getMessage());
            return false;
        }
        return true;
    }

    private boolean EditReader(Reader reader) {
         try {
         //    int amountBook =0;
            Connection cn = (Connection) MyConnect.getConnect();
        //     Statement stm = cn.createStatement();
       //      String sqlAmountBook = "select AmountBookIssued from Readers where ReaderID = "+reader.getReaderID();
       //       ResultSet rs = stm.executeQuery(sqlAmountBook);
       //       if(rs.next())
       //     {
      //            amountBook = rs.getInt(1);
      //        }
              
            String sql = "update Readers set FullName=?,PhoneNumber=?,Address=?,Career=? where ReaderID=? ";
             // String sql = "update Book set Title=?,Author=?,Sumary=?,CategoryID=?,Publisher=? where BookID=? ";
            PreparedStatement pst = cn.prepareStatement(sql);
            pst.setString(1, reader.getName());
            pst.setString(2, reader.getPhoneNumber());
            pst.setString(3, reader.getAddress());            
            pst.setString(4, reader.getCareer());   
            pst.setString(5, reader.getReaderID());
            pst.executeUpdate();
            pst.close();
            cn.close();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Edit Reader\n" + ex.getMessage());
            return false;
        }
        return true;
    }

    public String AddReader(Reader reader) {
        if (CheckReaderID(reader.getReaderID())) {
            return "Duplicate key ReaderID";
        }
        try {
            Connection cn = (Connection) MyConnect.getConnect();
            PreparedStatement pst =  cn.prepareStatement("insert Readers values(?,?,?,?,?,?)");
            pst.setString(1, reader.getReaderID());
            pst.setString(2, reader.getName());
            pst.setString(3, reader.getPhoneNumber());                        
            pst.setString(4, reader.getAddress());
            pst.setString(5, reader.getCareer());            
            pst.setInt(6, 0); // new reader co 0 quyen             
           
            //Chay lenh Insert
            pst.executeUpdate();
            pst.close();
            cn.close();
            return "";
        } catch (SQLException e) {
              System.out.println(e);
          //  JOptionPane.showMessageDialog(null, "AddBook\n" + e.getMessage());
            return e.toString();
        }
    }
     private boolean CheckReaderID(String readerID) {
        Connection con = (Connection) MyConnect.getConnect();
        try {
            String sql = "select * from Readers where ReaderID= '"+readerID+"';";
            Statement stm =  con.createStatement();
            ResultSet rs = stm.executeQuery(sql);
            if (rs.next()) {
               // JOptionPane.showMessageDialog(null, "Duplicate key BookID");
                return true;
            }
        } catch (Exception e) {
         //   JOptionPane.showMessageDialog(null, "checkBookID: " + e.getMessage());
            System.out.println(e);
            return true;
        }

        return false;
    }

    public  void searchReaderID() {
        try {
            String keyword = (String)inObject.readObject(); //nhan keyword-tu tim kiem tu client
            Reader reader = new Reader();
            reader = SearchReaderID(keyword);
            outObject.writeObject(reader);
        } catch (IOException | ClassNotFoundException ex) {
           System.out.println(ex);
        }
        
    
    }

    private Reader SearchReaderID(String keyword) {
          Connection cn = MyConnect.getConnect();                     //j la co` de co them cac sach co active bang false vao khong
    
           ServerCategory cate = new ServerCategory();
        String sqlBookID = "select * from Readers where ReaderID ='" + keyword + "'";
        try {
            Statement stm = cn.createStatement();
         
            ResultSet rs = null;    
            rs = stm.executeQuery(sqlBookID);     
            rs.next();
               Reader reader = new Reader(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5));
               reader.setAmountBookIssued(rs.getInt(6));
           
            
                return reader;
            
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Search by BookID: " + ex.getMessage());
            return null;
        }
    }


    
    
}
